package com.yjyw.tiexie.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.graphics.drawable.RoundedBitmapDrawable;
import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.load.resource.gif.GifDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.BitmapImageViewTarget;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.target.Target;
import com.bumptech.glide.request.transition.Transition;
import com.yjyw.tiexie.R;
import com.yjyw.tiexie.util.image.GlideCircleTransform;
import com.yjyw.tiexie.widget.GlideRoundTransform;
import com.luck.picture.lib.tools.ScreenUtils;

/**
 * Created on 2019/7/27.
 *
 * @author ${AUTHOR}
 * TODO:
 */
public class ImageLoadUtil {
    private static ImageLoadUtil instance;

    private ImageLoadUtil() {
    }

    public static ImageLoadUtil getInstance() {
        if (instance == null) {
            instance = new ImageLoadUtil();
        }
        return instance;
    }

    /**
     * 有加载中的图
     */
    public static void showImgWithDefault(ImageView imageView, Object url) {
        RequestOptions options = new RequestOptions();
        options.placeholder(R.drawable.default_2_1);
        options.error(R.drawable.default_2_1);
        Glide.with(imageView.getContext())
                .load(url)
                .skipMemoryCache(true) // 不使用内存缓存
                .diskCacheStrategy(DiskCacheStrategy.NONE) // 不使用磁盘缓存
                .apply(options)
                .into(imageView);
    }

    /**
     * 加载左右上边圆角图
     */
    public static void showImgWithAroundTop(ImageView imageView, Object url, int round) {
        RequestOptions options = new RequestOptions()
                .centerCrop()
                .transform(new GlideRoundTransform(round));
        options.placeholder(R.drawable.default_2_1);
        options.error(R.drawable.default_2_1);
        Glide.with(imageView.getContext())
                .load(url)
                .apply(options)
                .into(imageView);
    }

    /**
     * 加载长图比例图
     */
    public static void showLongImg(ImageView imageView, Object url) {
        Glide.with(imageView.getContext()).load(url).into(new SimpleTarget<Drawable>() {
            @Override
            public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                BitmapDrawable bd = (BitmapDrawable) resource;
                Bitmap bt = bd.getBitmap();
                int imageWidth = bt.getWidth();
                int imageHeight = bt.getHeight();
                int height = ScreenUtils.getScreenWidth(imageView.getContext()) * imageHeight / imageWidth;
                ViewGroup.LayoutParams para = imageView.getLayoutParams();
                para.height = height;
                para.width = ScreenUtils.getScreenWidth(imageView.getContext());
                imageView.setImageBitmap(bt);
            }
        });
    }


    /**
     * 有加载中的图
     */
    public static void showImgWithDefaultBanner(ImageView imageView, Object url) {
        imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
        RequestOptions options = new RequestOptions();
        Glide.with(imageView.getContext())
                .load(url)
                .skipMemoryCache(true) // 不使用内存缓存
                .diskCacheStrategy(DiskCacheStrategy.NONE) // 不使用磁盘缓存
                .apply(options)
                .into(imageView);
    }

    /**
     * 头像的图
     */
    public static void showImgWithDefaultHeader(ImageView imageView, Object url) {
        RequestOptions options = new RequestOptions();
        options.placeholder(R.drawable.face);
        options.error(R.drawable.face);
//        Glide.with(imageView.getContext())
//                .load(url)
//                .apply(options)
//                .into(imageView);
        Glide.with(imageView.getContext())
                .load(url)
                .skipMemoryCache(true) // 不使用内存缓存
                .diskCacheStrategy(DiskCacheStrategy.NONE) // 不使用磁盘缓存
                .apply(options)
                .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                .into(imageView);
    }

    /**
     * 头像的图
     */
    public static void showImgWithHeader(ImageView imageView, Object url) {
        RequestOptions options = new RequestOptions();
        options.placeholder(R.drawable.face);
        options.error(R.drawable.face);
        Glide.with(imageView.getContext())
                .load(url)
                .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                .into(imageView);
    }

    /**
     * 头像的图
     */
    public static void showImgWithDefaultOtherHeader(ImageView imageView, Object url) {
        RequestOptions options = new RequestOptions();
        options.placeholder(R.drawable.face);
        options.error(R.drawable.face);
        Glide.with(imageView.getContext())
                .load(url)
                .apply(options)
                .into(imageView);
    }
    /**
     * 头像的图
     */
    public static void showImgWithBlueHeader(ImageView imageView, Object url) {
        RequestOptions options = new RequestOptions();
        options.placeholder(R.drawable.icon_default_header_blue);
        options.error(R.drawable.icon_default_header_blue);
        Glide.with(imageView.getContext())
                .load(url)
                .apply(options)
                .into(imageView);
    }


    /**
     * 有加载中的圆角8图
     */
    public static void showImgWithDefault8(ImageView imageView, Object url) {
//        RequestOptions requestOptions = new RequestOptions();
//        requestOptions.placeholder(R.drawable.default_2_1);
//        requestOptions.circleCropTransform();
//        requestOptions.transforms(new RoundedCorners(26));
//        Glide.with(imageView.getContext()).load(url).apply(requestOptions).into(imageView);

        RequestOptions options = new RequestOptions()
                .centerCrop();
        Glide.with(imageView.getContext())
                .asBitmap()
                .load(url)
                .apply(options)
                .skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.NONE)
                .into(new BitmapImageViewTarget(imageView) {
                    @Override
                    protected void setResource(Bitmap resource) {
                        try {
                            RoundedBitmapDrawable circularBitmapDrawable =
                                    RoundedBitmapDrawableFactory.create(imageView.getContext().getResources(), resource);
                            circularBitmapDrawable.setCornerRadius(8);
                            imageView.setImageDrawable(circularBitmapDrawable);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    /**
     * 有加载中的圆角8图 使用内存缓存
     */
    public static void showImgWithDefault8Local(ImageView imageView, Object url) {

        RequestOptions requestOptions = new RequestOptions();
        requestOptions.placeholder(R.drawable.default_2_1);
        requestOptions.circleCropTransform();
        requestOptions.transforms(new RoundedCorners(26));
        Glide.with(imageView.getContext()).load(url).skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.NONE).apply(requestOptions).into(imageView);
    }

    /**
     * 有加载中的圆角8图
     */
    public static void showImgWithCorner(ImageView imageView, Object url, int corner) {

        RequestOptions options = new RequestOptions()
                .centerCrop();
        Glide.with(imageView.getContext())
                .asBitmap()
                .load(url)
                .skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.NONE)
                .apply(options)
                .into(new BitmapImageViewTarget(imageView) {
                    @Override
                    protected void setResource(Bitmap resource) {
                        try {
                            RoundedBitmapDrawable circularBitmapDrawable =
                                    RoundedBitmapDrawableFactory.create(imageView.getContext().getResources(), resource);
                            circularBitmapDrawable.setCornerRadius(ViewUtils.dip2px(imageView.getContext(),corner));
                            imageView.setImageDrawable(circularBitmapDrawable);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    /**
     * 加载圆角图,显示头像
     */
    public static void displayCircle(ImageView imageView, String imageUrl) {
        RequestOptions options = new RequestOptions();
        options.placeholder(R.drawable.default_header);
        options.error(R.drawable.default_header);
        options.transform(new GlideCircleTransform());

        Glide.with(imageView.getContext())
                .load(imageUrl)
                .apply(options)
                .into(imageView);
    }

    /**
     * 加载圆角图,显示头像
     */
    public static void displayCircleProduct(ImageView imageView, String imageUrl) {
        RequestOptions options = new RequestOptions();
        options.placeholder(R.drawable.icon_default_header_contact);
        options.error(R.drawable.icon_default_header_contact);
        options.transform(new GlideCircleTransform());

        Glide.with(imageView.getContext())
                .load(imageUrl)
                .apply(options)
                .into(imageView);
    }

    public static void loadRound8dpPic(Context context, Object path, ImageView targetIv) {
        if (context == null) return;
        RoundedCorners roundedCorners = new RoundedCorners((int) DisplayUtil.dp2px(context, 8));
        RequestOptions options = new RequestOptions().transform(new CenterCrop(), roundedCorners);
        Glide.with(context)
                .load(path)
                .placeholder(android.R.color.white)
                .apply(options)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(targetIv);
    }


    /**
     * 加载圆角图,显示头像
     */
    public static void displayGif(ImageView imageView, int imageUrl) {
        RequestOptions options = new RequestOptions().skipMemoryCache(false);//配置
        Glide.with(imageView.getContext()).load(imageUrl).apply(options).listener(new RequestListener() {
            @Override
            public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) {
                return false;
            }

            @Override
            public boolean onResourceReady(Object resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) {
                if (resource instanceof GifDrawable) {
                    //加载一次
//                    ((GifDrawable) resource).setLoopCount(1);
                }
                return false;
            }

        }).into(imageView);
    }
}
